package com.me.libgdx_test;

import java.util.ArrayList;

import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.me.libgdx_test.ClassUnlockablePrototypes.UnlockFactory;
import com.me.libgdx_test.ClassUnlockablePrototypes.UnlockPrototype;

//This class works much like the classMarketDirector on the market screen
//Arranges all the sprites and adds click actions to them
public class ClassSelectionDirector {
	private int race_index, level_index, class_index, current_index, mod_index, enemies_index, spells_index, gods_index;
	private String race_name, level_name, class_name, mod_name, enemies_name, spells_name, gods_name;
	private String race_description, level_description, class_description, mod_description, enemies_description, spells_description, gods_description;
	private String button_text;
	private Label description_label,title_label;
	private ClassGalleryActor races, levels, classes, mods, enemies, spells, gods;
	private ClassLogDisplay logger;
	private ArrayList<Integer> class_list,level_list,race_list, mod_list, enemies_list, spells_list, gods_list;
	private int starting_index;
	//This class will hold all the info for the selection screen
	ClassSelectionDirector(Label title_label, Label description_label, boolean game_info, int starting_index){
		//This needs to be a pointer, so it stays attached to the stage
		this.title_label = title_label;
		this.description_label = description_label;
		race_index = -1;
		level_index = -1;
		class_index = -1;
		mod_index = -1;
		enemies_index = -1;
		spells_index = -1;
		gods_index = -1;
		button_text = "Select Race";
		logger  = new ClassLogDisplay("ClassSelectionDirector");
		if(!game_info){
			this.title_label.setText("Choose Race");
		}
		this.starting_index = starting_index;
		current_index = starting_index;
	}
	public Label get_title(){
		return title_label;
	}
	public Label get_description(){
		return description_label;
	}
	public void set_vars(int index, String name, String description){
		switch(current_index){
		case 0:
			this.race_index = index;
			this.race_name = name;
			this.race_description = description;
			break;
		case 1:
			this.level_index = index;
			this.level_name = name;
			this.level_description = description;
			break;
		case 2:
			this.class_index = index;
			this.class_name = name;
			this.class_description = description;
			break;
		case 3:
			this.mod_index = index;
			this.mod_name = name;
			this.mod_description = description;
			break;
		case 4:
			this.enemies_index = index;
			this.enemies_name = name;
			this.enemies_description = description;
			break;
		case 5:
			this.spells_index = index;
			this.spells_name = name;
			this.spells_description = description;
			break;
		case 6:
			this.gods_index = index;
			this.gods_name = name;
			this.gods_description = description;
			break;
		}
		
	}
	
public void set_actors(ClassDungeonLevel[] game_vars, ClassGalleryActor races, ClassGalleryActor levels, ClassGalleryActor classes, 
		ClassGalleryActor mods, ClassGalleryActor enemies, ClassGalleryActor spells, ClassGalleryActor gods, UnlockFactory unlocked_things){
		
		//This filters all the unlocked stuff so we only see the things we are allowed to see
		/*
		 * 		race_index = -1;
		level_index = -1;
		class_index = -1;
		 */
	
		set_actors(races, levels, classes, unlocked_things);
	
		//We need to add the mods to the enemy descriptions here
		
		//This is repetative because the level array has already been iterated through below...
		enemies_list = new ArrayList<Integer>();
		for(UnlockPrototype unlock_level:unlocked_things.get_levels()){
			for(int monster: game_vars[unlock_level.value].monsters){
				logger.print("Unlocked Enemies: " + monster);
				if(enemies_index == -1){
					enemies_index = monster;
				}
				if(!enemies_list.contains(monster)){
					enemies_list.add(monster);
				}
			}
		}
		enemies.filter_references(enemies_list);
		
		/*mod_list = new ArrayList<Integer>();
		for(UnlockPrototype unlock_race:unlocked_things.get_races()){
			if(mod_index == -1){
				mod_index = unlock_race.value;
			}
			mod_list.add(unlock_race.value);
		}
		mods.filter_references(mod_list);
		
		enemies_list = new ArrayList<Integer>();
		for(UnlockPrototype unlock_level:unlocked_things.get_levels()){
			logger.print("Unlocked Levels: " + unlock_level.value);
			if(level_index == -1){
				level_index = unlock_level.value;
			}
			level_list.add(unlock_level.value);
		}
		levels.filter_references(level_list);*/
		
		spells_list = new ArrayList<Integer>();
		for(UnlockPrototype unlock_spell:unlocked_things.get_spells()){
			if(spells_index == -1){
				spells_index = unlock_spell.value;
			}
			spells_list.add(unlock_spell.value);
		}
		spells.filter_references(spells_list);
		
		/*gods_list = new ArrayList<Integer>();
		for(UnlockPrototype unlock_class:unlocked_things.get_classes()){
			if(class_index == -1){
				class_index = unlock_class.value;
			}
			class_list.add(unlock_class.value);
			logger.print("Class: "+ unlock_class.value);
		}
		classes.filter_references(class_list);*/
		
		//This saves all the elements into the director so that they can be shown on the screen
		this.mods = mods; 
		this.enemies = enemies;
		this.spells = spells; 
		this.gods = gods;
	}
	
	public void set_actors(ClassGalleryActor races, ClassGalleryActor levels, ClassGalleryActor classes, UnlockFactory unlocked_things){
		
		//This filters all the unlocked stuff so we only see the things we are allowed to see
		/*
		 * 		race_index = -1;
		level_index = -1;
		class_index = -1;
		 */
		
		race_list = new ArrayList<Integer>();
		for(UnlockPrototype unlock_race:unlocked_things.get_races()){
			if(race_index == -1){
				race_index = unlock_race.value;
			}
			race_list.add(unlock_race.value);
		}
		races.filter_references(race_list);
		
		level_list = new ArrayList<Integer>();
		for(UnlockPrototype unlock_level:unlocked_things.get_levels()){
			logger.print("Unlocked Levels: " + unlock_level.value);
			if(level_index == -1){
				level_index = unlock_level.value;
			}
			level_list.add(unlock_level.value);
		}
		levels.filter_references(level_list);
		
		class_list = new ArrayList<Integer>();
		for(UnlockPrototype unlock_class:unlocked_things.get_classes()){
			if(class_index == -1){
				class_index = unlock_class.value;
			}
			class_list.add(unlock_class.value);
			logger.print("Class: "+ unlock_class.value);
		}
		classes.filter_references(class_list);
		
		//This saves all the elements into the director so that they can be shown on the screen
		this.races = races;
		this.levels = levels;
		this.classes = classes;
	}
	public ClassGalleryActor get_gallery(){
		switch(current_index){
		case 0:return races;
		case 1:return levels;
		case 2:return classes;
		case 3:return mods;
		case 4:return enemies;
		case 5:return spells;
		case 6:return gods;
		//ClassGalleryActor mods, ClassGalleryActor enemies, ClassGalleryActor spells, ClassGalleryActor gods
		default:return races;
		}
	}
	public void set_label_text(boolean details, int completes_left){
		String complete_text = "\nCompletes For Next Level: " + completes_left;
		String text;
		if(details){
			switch(current_index){
				case 0:text = "Details: \n\nRace: " + race_name + "\nRace Details: " + race_description;break;
				case 1:text = "Details: \n\nRace: " + race_name + "\nLevel: " + level_name + "\nLevel Description: \n" + level_description + complete_text;break;
				case 2:text = "Details: \n\nRace: " + race_name + "\nLevel: " + level_name + "\nClass: " + class_name + "\nClass Description: " + class_description + "\n" +complete_text;break;
				//case 3:description_label.setText("Details: \n\nRace: " + race_name + "\nLevel: " + level_name + "\nClass: " + class_name + "\nClass Description: " + class_description);
				default:text = "Details: ";break;
			}
		}else{
			switch(current_index){
			case 0:text = "Race: " + race_name + "\n\nRace Details: " + race_description;break;
			case 1:text = "Level: " + level_name + "\n\nLevel Description: \n" + level_description;break;
			case 2:text = "Class: " + class_name + "\n\nClass Description: " + class_description;break;
			case 3:text = "Mod: " + mod_name + "\nMod Description: " + mod_description;break;
			case 4:text = "Enemy: " + enemies_name + "\n" + enemies_description;break;
			case 5:text = "Spell: " + spells_name + "\n\nSpell Description: " + spells_description;break;
			case 6:text = "God: " + gods_name + "\n\nGod Description: " + gods_description;break;
			//case 3:description_label.setText("Details: \n\nRace: " + race_name + "\nLevel: " + level_name + "\nClass: " + class_name + "\nClass Description: " + class_description);
			default:text = "Details: ";break;
		}
		}
		description_label.setText(text);
	}
	public int get_starting_index(){
		if(current_index == 1){
			return levels.get_actors().size()-1;
		}
		return 0;
	}
	public void iterate_previous(){
		switch(current_index){
		case 0:button_text = "Select Race";title_label.setText("Choose Race");break;
		case 1:current_index--;button_text = "Select Race";title_label.setText("Choose Race");break;
		case 2:current_index--;button_text = "Select Level";title_label.setText("Choose Level");break;
		default:break;
		}
	}
	public void iterate_next(){
		switch(current_index){
		case 0:current_index++;button_text = "Select Level";title_label.setText("Choose Level");break;
		case 1:current_index++;button_text = "Enter Dungeon";title_label.setText("Choose Class");break;
		//case 2:current_index++;button_text = "Enter Dungeon";break;
		default:break;
		}
	}
	public int get_race(){
		return race_list.get(race_index);
	}
	public int get_level(){
		return level_list.get(level_index);
	}
	public int get_class(){
		return class_list.get(class_index);
	}
	public int get_mod(){
		return mod_list.get(mod_index);
	}
	public int get_enemy(){
		return enemies_list.get(enemies_index);
	}
	public int get_spell(){
		return spells_list.get(spells_index);
	}
	public int get_god(){
		return gods_list.get(gods_index);
	}
	public void reset_index(){
		current_index = starting_index;
	}
	public void set_index(int current_index){
		this.current_index = current_index;
	}
	public int get_index(){
		return current_index;
	}
	public String button_text(){
		return button_text;
	}
}
